package com.windoent.yugioh.utils;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.windoent.yugioh.MyApplication;
import com.windoent.yugioh.R;
import com.windoent.yugioh.constants.Constants;
import com.windoent.yugioh.ui.activity.registlogin.LoginActivity;
import com.windoent.yugioh.widget.dialog.DeleteSureDialog;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * author : hp
 * date   : 2020/4/1 13:29
 * desc   :基础工具类
 */
public class BaseUtils {

    //    判断字符串是否为json格式
    public final static boolean isJSONValid(String jsonInString) {
        try {
            Gson gson = new Gson();
            gson.fromJson(jsonInString, Object.class);
            return true;
        } catch (JsonSyntaxException ex) {
            return false;
        }
    }

    public static boolean codeIs0(String response) throws JSONException {
        if (response != null) {
            JSONObject jsonObject = new JSONObject(response);
            String code = jsonObject.getString("code");
            if (!TextUtils.isEmpty(code) && (code.equals("200"))) {
                return true;
            } else {
                String msg = jsonObject.getString("msg");
                if (!TextUtils.isEmpty(msg))
                    ToastUtils.showText(msg);
                return false;
            }
        }
        return false;
    }

    //token失效跳转登录
    public static boolean codeIs0(Context context, String response) throws JSONException {
        if (response != null) {
            JSONObject jsonObject = new JSONObject(response);
            String code = jsonObject.getString("code");
            if (!TextUtils.isEmpty(code) && (code.equals("200"))) {
                return true;
            } else {
                String msg = jsonObject.getString("msg");
                if (!TextUtils.isEmpty(msg))
                    ToastUtils.showText(msg);
                if (BaseMethod.notNull(code) && code.equals("10314")) {
                    toLogIn(context, msg);
                }
                return false;
            }
        }
        return false;
    }

    //token验证失败
    public static void toLogIn(Context context, String msg) {
        DeleteSureDialog dialog = new DeleteSureDialog(context);
        dialog.setTvContent(BaseMethod.notNullDefaultValue(msg, context.getString(R.string.string_please_login_app)));
        dialog.setYesOnclickListener(new DeleteSureDialog.onYesOnclickListener() {
            @Override
            public void onYesClick() {
                SharedPreUtil.put(context, Constants.APPTOKEN, "");
                SharedPreUtil.put(context, Constants.USERID, 0);
                MyApplication.getInstance().cleanLoginInfo();
                MyApplication.getInstance().cleanMsgNumber();
                context.startActivity(new Intent(context, LoginActivity.class));
            }
        });
        dialog.show();
    }

    /**
     * 字符串转int
     *
     * @param string
     * @return
     */
    public static int stringToInt(String string) {
        try {
            if (BaseMethod.notNull(string))
                return Double.valueOf(string).intValue();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return 0;
    }

    //字符串转double
    public static double stringToDouble(String s) {
        if (!TextUtils.isEmpty(s)) {
            try {
                return Double.parseDouble(s);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return 0;
    }

    //小数位数为2
    public static String double2String3(double doub) {

        return (double) Math.round(doub * 1000) / 1000 + "";
    }
}
